import fetch from 'common/src/utils/fetch'
import UTILS from "common/src/utils/utils";


/**
 * 删除
 */
function delete_(data, calendarId, that) {
    let datas = [];
    for (const d of data) {
        datas.push({
            calendarId: calendarId,
            studentId: d.studentId, 
            teachingClassId: d.teachingClassId,
            turn: d.turn
        });
    }
    if (datas.length < 1) {
        that.$noSelectAlert();
        return;
    }
    const promise = new Promise(function(resolve, reject) {
        that.$delConfirm("您确定要退课吗？", "退课").then(() => {
            fetch({
                url: `/api/electionservice/elcCourseTake`,
                method: 'delete',
                data: datas
            }).then((res) => {
                resolve(res);
            },(error)=>{
                reject(error);
            });
        },(error)=>{
            reject(error);
        });
    });
    return promise;
}


/**
 * 新增
 */
function add(data) {
    return fetch({
        url: `/api/electionservice/elcCourseTake/addCourseBk`,
        method: 'post',
        data: data
    });
}

/**
 * 
 * 批量加课获取状态
 */
function addStatus(key) {
    return fetch({
        url: '/api/electionservice/elcCourseTake/findBatchAddStatus?key=' + key,
        method: 'get'
    });
}

function increase(data) {
    return fetch({
        url: `/api/electionservice/elcCourseTake`,
        method: 'put',
        data: data
    });
}

function trans(data) {
    return fetch({
        url: `/api/electionservice/elcResult/changeStudentClass`,
        method: 'post',
        data: data
    });
}

function download(data) {
    return fetch({
        url: `/api/electionservice/elcCourseTake/export`,
        method: 'post',
        data: data,
    });
    // .then(res => {
    //     UTILS.downloadFile(res, '上课名单.xls', 'application/msword;charset=utf-8');
    // });
}


function bkCrossCampusAllStudentByCampus(data) {
    return fetch({
        url: `/api/electionservice/elcCourseTake/exportBkCrossCampusAllStudentByCampus`,
        method: 'post',
        data: data,
        responseType: 'blob'
    }).then(res => {
        UTILS.downloadFile(res, '周末跨校区上课名单.xls', 'application/vnd.ms-excel;charset=utf-8');
    });;
}

function bkAllStudentByClass(data) {
    return fetch({
        url: `/api/electionservice/elcCourseTake/exportBkAllStudentByClass`,
        method: 'post',
        data: data,
        responseType: 'blob'
    }).then(res => {
        UTILS.downloadFile(res, '课表中班级所有学生名单.xls', 'application/vnd.ms-excel;charset=utf-8');
    });
}

function editStudyType(data) {
    return fetch({
        url: `/api/electionservice/elcCourseTake/editStudyType`,
        method: 'post',
        data:data,
    });
}

/**
 * 研究生加课
 */
function graduateAdd(data) {
    return fetch({
        url: `/api/electionservice/elcCourseTake/addCourse`,
        method: 'post',
        data: data
    });
}

/**
 * 研究生冲突加课
 */
function graduateForceAdd(data) {
    return fetch({
        url: `/api/electionservice/elcCourseTake/forceAdd`,
        method: 'post',
        data: data
    });
}

/**
 * 研究生退课
 */
function graduateDelete(data) {
    return fetch({
        url: `api/electionservice/elcCourseTake/removedCourse`,
        method: 'post',
        data: data
    });
}

/**
 * 研究生导出学生课程
 */
function graduateExportCourse(ids) {
    return fetch({
        url: '/api/electionservice/elcCourseTake/exportGraduatePage',
        method: 'post',
        data: ids,
        responseType: 'blob'
    }).then(res => {
        UTILS.downloadFile(res, '学生选课记录.xls', 'application/vnd.ms-excel;charset=utf-8');
    });
}

/**
 * 研究生导出学生全部课程
 */
function graduateExportAllSelectedCourse(data, studentName) {
    return fetch({
        url: `/api/electionservice/elcCourseTake/exportAllSelectedCourse`,
        method: 'post',
        data: data,
        responseType: 'blob'
    }).then(res => {
        UTILS.downloadFile(res, '学生选课记录-'+ studentName + '.xls', 'application/vnd.ms-excel;charset=utf-8');
    });
}

/**
 * 导出选课学生名单
 */
function exportStudent(val) {
    let data ={}
    data.pageNum_ = 0
    data.pageSize_ = 0
    data.condition = val
    return fetch({
        url: `/api/electionservice/elcCourseTake/practiceList/student/excel`,
        method: 'post',
        data: data,
        responseType: 'blob'
    }).then(res => {
        UTILS.downloadFile(res, '应选学生名单.xls', 'application/vnd.ms-excel;charset=utf-8');
    });
}


function findRole() {
    return fetch({
      url: '/api/electionservice/electionConstants/findElcResultFlag',
      method: 'GET'
    })
}

function findWithdrawRole() {
    return fetch({
      url: '/api/electionservice/electionConstants/findElcWithdrawFlag',
      method: 'GET'
    })
}

/**
 * 上传文件
 */
function bkUpload(data) {
    return fetch({
        url: `api/electionservice/elcCourseTake/bkUpload`,
        method: 'post',
        data: data
    });
}

/**
 * 判断学期
 */
function getIsNestCalendarId(data) {
    return fetch({
        url: `api/electionservice/electionRound/getIsNestCalendarId`,
        method: 'post',
        data: data
    });
}

/**
 * 查询加课学生
 */
function findAddCourseStu(data) {
    return fetch({
        url: `/api/electionservice/elcCourseTake/studentPage`,
        method: 'post',
        data: data
    });
}

/**
 * 查询承诺书
 * @param {*} calendarId 
 */
function getCommitmentSetting(calendarId) {
    return fetch({
      url: '/api/electionservice/elecCommitment/getCommitmentSetting/'+ calendarId,
      method: 'GET'
    })
}

/**
 * 查询学生承诺书签署状态
 * @param {*} calendarId 
 */
function getCommitmentStatus(calendarId,studentId) {
    return fetch({
      url: '/api/electionservice/elecCommitment/getCommitmentStudent/'+ calendarId + '/' + studentId,
      method: 'GET'
    })
}

/**
 * 管理员保存承诺书
 */
function saveCommitmentSetting(data) {
    return fetch({
        url: `/api/electionservice/elecCommitment/saveCommitmentSetting`,
        method: 'post',
        data: data
    });
}

/**
 * 学生保存承诺书
 */
function saveCommitmentStudent(data) {
    return fetch({
        url: `/api/electionservice/elecCommitment/saveCommitmentStudent`,
        method: 'post',
        data: data
    });
}
/**
 * 学生保存承诺书
 */
function asyncAutoChangeWithdraw(){
    return fetch({
        url: `/api/electionservice/elcCourseTake/changeWithdraw`,
        method: 'post'
    });
}
export default {
    "delete_": delete_,
    "add": add,
    "download":download,
    "practicalUrl": "/api/electionservice/elcCourseTake/practiceList/student",
    "pageUrl":"/api/electionservice/elcCourseTake/page",
    "historyPageUrl":'/api/electionservice/elcLog/page',
    "coursePageUrl":"/api/electionservice/elcCourseTake/coursePage",
    "uploadUrl":"/api/electionservice/elcCourseTake/bkUpload",
    "downloadUrl":"/api/electionservice/elcCourseTake/download",
    "findStuInfoPageUrl":"/api/electionservice/elcCourseTake/studentPage",
    "editStudyType":editStudyType,
    "graduatePageUrl":"/api/electionservice/elcCourseTake/graduatePage",
    "graduateAddCourseListUrl":"/api/electionservice/elcCourseTake/addCourseList",
    "graduateAllSelectedCourseUrl": "/api/electionservice/elcCourseTake/allSelectedCourse",
    "graduateAdd": graduateAdd,
    "graduateForceAdd": graduateForceAdd,
    "graduateDelete": graduateDelete,
    "graduateExportCourse": graduateExportCourse,
    "graduateExportAllSelectedCourse": graduateExportAllSelectedCourse,
    'trans': trans,
    'increase': increase,
    'exportStudent':exportStudent,
    'findRole': findRole,
    "bkUpload": bkUpload,
    "getIsNestCalendarId": getIsNestCalendarId,
    findWithdrawRole: findWithdrawRole,
    "findAddCourseStu": findAddCourseStu,
    getCommitmentSetting: getCommitmentSetting,
    saveCommitmentSetting: saveCommitmentSetting,
    getCommitmentStatus: getCommitmentStatus,
    saveCommitmentStudent: saveCommitmentStudent,
    addStatus:addStatus,
    'asyncAutoChangeWithdraw':asyncAutoChangeWithdraw,
    "bkCrossCampusAllStudentByCampus":bkCrossCampusAllStudentByCampus,
     bkAllStudentByClass
}
